package gui;

import domain.server.S19Server;
import domain.client.SundayThe19thGame;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author Laurent Albessart
 * @version 31-dec-2011
 */
public class PickOptionWindow extends JFrame{
    
    public PickOptionWindow(){
        super("testing window");
        setLayout(new FlowLayout());
        
        JButton makeServerButton = new JButton(new AbstractAction("Make a game") {

            @Override
            public void actionPerformed(ActionEvent ae) {
                //TODO setup game and add it to lobby, connect player to game
                //Create server
                ((JButton) ae.getSource()).setEnabled(false);
                S19Server server = new S19Server();
                
                new Thread(server).start();
                
                //Check server, connect to it and create game
            }
        });
        add(makeServerButton);
        
        
        JButton joinGameButton = new JButton(new AbstractAction("Join a game") {

            @Override
            public void actionPerformed(ActionEvent ae) {
                //TODO ask ip (lateron replaced by click ip), connect player to game
                
                //Ask name
                String name = JOptionPane.showInputDialog("Please enter a nickname");
                
                //Ask ip
                String ipAndHost = JOptionPane.showInputDialog("Please enter the IP + port (xxx.xxx.xxx.xxx:56565");
                try {
                    //Check server, connect to it and create game
                    
                    String[] split = ipAndHost.split(":");
                    
                    InetAddress ip = InetAddress.getByName(split[0]);
                    
                    //Use ip to set up game
                    new SundayThe19thGame(name, ip, Integer.parseInt(split[1]));
                    
                } catch (UnknownHostException ex) {
                    Logger.getLogger(PickOptionWindow.class.getName()).log(Level.SEVERE, null, ex);
                    //TODO invalid IP, give message!
                }
            }
        });
        add(joinGameButton);
        
        
        setSize(100, 100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    
}
